package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.query.DestinationThemeQuery;
import cn.wolfcode.luowowo.article.query.StrategyCatalogQuery;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.*;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.time.Month;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("destination")
public class DestinationController {
    @Reference
    private IRegionService regionService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyCatalogService strategyCatalogService;
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private ITravelService travelService;
    @Reference
    private IDestinationThemeService destinationThemeService;
    @Reference
    private IDestinationDaysService destinationDaysService;
    @Reference
    private IDestinationTimeService destinationTimeService;
    @Reference
    private IScenicspotService scenicspotService;


    @RequestMapping("")
    public String index(Model model){
        List<Region> hotRegions =  regionService.getHotRegions();
        model.addAttribute("hotRegions",hotRegions);
        List<DestinationTime> months = destinationTimeService.selectByType(1);
        model.addAttribute("months",months);
        return "destination/index";
    }

    @RequestMapping("destFilter")
    public String destFilter(Model model,Long themeId,Long timeId){
//        查询月份
        List<DestinationTime> months = destinationTimeService.selectByType(1);
        model.addAttribute("months",months);
//        查询节假日
        List<DestinationTime> holiday = destinationTimeService.selectByType(4);
        model.addAttribute("holiday",holiday);
//        查询主题：全年适宜
        List<DestinationTheme> allyear = destinationThemeService.selectByType(1);
        model.addAttribute("allyear",allyear);
//        查询主题：季节
        List<DestinationTheme> season = destinationThemeService.selectByType(2);
        model.addAttribute("season",season);
//        查询主题：出行方式
        List<DestinationTheme> methods = destinationThemeService.selectByType(3);
        model.addAttribute("methods",methods);
//        查询天数
        List<DestinationDays> allDays = destinationDaysService.query();
        model.addAttribute("allDays",allDays);
        model.addAttribute("themeId",themeId);
        model.addAttribute("timeId",timeId);
        return "destination/destFilter";
    }

    @RequestMapping("getDestByThemeId")
    public String getThemeById(Model model,@ModelAttribute("qo") DestinationThemeQuery qo){
        qo.setPageSize(6);
        PageInfo pageInfo = destinationService.queryByCondition(qo);
        model.addAttribute("pageInfo",pageInfo);
        List<Scenicspot> list = scenicspotService.getTop3();
        model.addAttribute("list",list);
        return "destination/destTpl";
    }

    @RequestMapping("getDestByMonthId")
    public String getDestByMonthId(Model model,Long id){
        List<Destination> dest = destinationService.getDestByMonthId(id);
        List<Destination> llist = dest.subList(0,3);
        model.addAttribute("llist",llist);
        List<Destination> rlist = dest.subList(3,6);
        model.addAttribute("rlist",rlist);
        model.addAttribute("timeId",id);
        return "destination/destMonthTpl";
    }

    @RequestMapping("getThemeByType")
    public String getThemeByType(Model model,int type){
        List<DestinationTheme> dest = destinationThemeService.selectByType(type);
        model.addAttribute("dest",dest);
        return "destination/themeTpl";
    }

    @RequestMapping("getThemeByTimeType")
    public String getThemeByTimeType(Model model,int type){
        List<DestinationTime> holiday = destinationTimeService.selectByType(type);
        model.addAttribute("holiday",holiday);
        return "destination/holidayTpl";
    }

    @RequestMapping("getHotDestByRegionId")
    public String getHotDestByRegionId(Model model,@ModelAttribute("regionId") Long regionId){
        List<Destination> dests =  destinationService.getHotDestByRegionId(regionId);
        if(dests.size() > 10){
            dests.subList(0, 10);
        }
        List<Destination> leftDests = dests.subList(0, dests.size() / 2 + 1);
        List<Destination> rightDests = dests.subList(dests.size() / 2 + 1, dests.size());
        model.addAttribute("leftDests",leftDests);
        model.addAttribute("rightDests",rightDests);
        return "destination/hotdestTpl";
    }

    @RequestMapping("guide")
    public String guide(Model model,Long id){
        List<Destination> toasts = destinationService.getToasts(id);
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("dest",dest);
        model.addAttribute("toasts",toasts);
        model.addAttribute("catalogs",strategyCatalogService.getCatalogsByDestId(id));
        model.addAttribute("strategyDetails",strategyDetailService.getViewnumTop3DetailsByDestId(id));
        return "destination/guide";
    }

    @RequestMapping("surveyPage")
    public String surveyPage(Model model,@ModelAttribute("qo") StrategyCatalogQuery qo){
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts",toasts);
        model.addAttribute("dest",dest);
        return "destination/survey";
    }

    @RequestMapping("survey")
    public String survey(Model model,@ModelAttribute("qo") StrategyCatalogQuery qo){
        model.addAttribute("catalogs",strategyCatalogService.getCatalogsByDestId(qo.getDestId()));
        StrategyCatalog catalog = strategyCatalogService.get(qo.getCatalogId());
        model.addAttribute("catalog",catalog);
        List<StrategyDetail> details = strategyDetailService.getDetailsByCatalogId(qo.getCatalogId());
        model.addAttribute("detail",details.get(0));
        return "destination/surveyTpl";
    }

    @RequestMapping("travels")
    public String travels(Model model,@ModelAttribute("qo") TravelQuery qo){
        model.addAttribute("pageInfo",travelService.query(qo));
        return "destination/travelTpl";
    }
}
